/**
 * 
 */
package com.gosophia.conferenceManagement.starting.ws.impl;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.gosophia.commons.exception.BusinessException;
import com.gosophia.conferenceManagement.starting.dto.ConferenceStartingDTO;
import com.gosophia.conferenceManagement.starting.entity.ConferenceStartingTransientStore;
import com.gosophia.conferenceManagement.starting.service.ConferenceTransientStoreService;
import com.gosophia.conferenceManagement.starting.ws.ConferenceTransientStoreWebService;
import com.gosophia.dataDictionary.entity.DataValue;
import com.gosophia.dataDictionary.service.DataDictionaryService;
import com.gosophia.taskManagement.entity.Job;
import com.gosophia.taskManagement.service.TaskManagementService;
import com.gosophia.userManagement.entity.UserInfo;
import com.gosophia.userManagement.service.UserManagementService;

/**
 * 功能： 会议发起暂存webService实现
 * 
 * @author tianchunyu
 * @since 2011-3-23 Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Service("ConferenceTransientStoreWebServiceimpl")
@Transactional
public class ConferenceTransientStoreWebServiceimpl implements
		ConferenceTransientStoreWebService {

	@Autowired
	private TaskManagementService taskManagementService;
	@Autowired
	private UserManagementService userManagementService;

	@Autowired
	private DataDictionaryService dataDictionaryService;

	@Autowired
	private Mapper dozer;

	@Autowired
	private ConferenceTransientStoreService conferenceTransientStoreService;

	/**
	 * 根据操作人查
	 * 
	 * @throws BusinessException
	 */
	@Override
	public ConferenceStartingDTO findTransientInfoByCreator(Long userInfoId)
			throws BusinessException {
		Assert.isTrue(userInfoId != null, "创建人编号不能为null");
		ConferenceStartingTransientStore conferenceStartingTransientStore = conferenceTransientStoreService
				.findTransientInfoByCreator(userInfoId);
		if (conferenceStartingTransientStore == null) {
			return null;
		}
		return makeEntityToDto(conferenceStartingTransientStore);
	}

	/**
	 * 根据任务id查
	 * 
	 * @throws BusinessException
	 */
	@Override
	public ConferenceStartingDTO findTransientInfoByJobId(Long jobId)
			throws BusinessException {
		Assert.isTrue(jobId != null, "任务Id不能为null");
		ConferenceStartingTransientStore conferenceStartingTransientStore = conferenceTransientStoreService
				.findTransientInfoByJobId(jobId);
		if (conferenceStartingTransientStore == null) {
			return null;
		}
		return makeEntityToDto(conferenceStartingTransientStore);
	}

	@Override
	public ConferenceStartingDTO saveTransientInfo(
			ConferenceStartingDTO conferenceStartingDTO)
			throws BusinessException {
		Assert.isTrue(conferenceStartingDTO != null, "要存储的对象不能为null");
		Assert.isTrue(
				conferenceStartingDTO.getConferenceStartingDTOId() == null,
				"要存储的对象id必须为null");

		ConferenceStartingTransientStore conferenceStartingTransientStore = conferenceTransientStoreService
				.saveTransientInfo(makeDtoToEntity(conferenceStartingDTO));
		return makeEntityToDto(conferenceStartingTransientStore);
	}

	@Override
	public ConferenceStartingDTO updateTransientInfo(
			ConferenceStartingDTO conferenceStartingDTO)
			throws BusinessException {
		Assert.isTrue(conferenceStartingDTO != null, "要更新的对象不能为null");
		Assert.isTrue(
				conferenceStartingDTO.getConferenceStartingDTOId() != null,
				"要更新的对象id不能为null");
		ConferenceStartingTransientStore conferenceStartingTransientStore = conferenceTransientStoreService
				.updateTransientInfo(makeDtoToEntity(conferenceStartingDTO));
		return makeEntityToDto(conferenceStartingTransientStore);

	}

	private ConferenceStartingTransientStore makeDtoToEntity(
			ConferenceStartingDTO conferenceStartingDTO) {
		if (conferenceStartingDTO == null) {
			return null;
		}
		ConferenceStartingTransientStore conferenceStartingTransientStore = new ConferenceStartingTransientStore();
		conferenceStartingTransientStore = dozer.map(conferenceStartingDTO,
				ConferenceStartingTransientStore.class);

		if (conferenceStartingDTO.getConferenceStartingDTOId() != null) {
			conferenceStartingTransientStore
					.setTransientStoreId(conferenceStartingDTO
							.getConferenceStartingDTOId());
		}
		if (conferenceStartingDTO.getConferenceCreatorId() != null) {
			UserInfo conferenceCreator = userManagementService
					.findUserInfoByUserInfoId(conferenceStartingDTO
							.getConferenceCreatorId());
			conferenceStartingTransientStore
					.setConferenceCreator(conferenceCreator);
		}
		if (conferenceStartingDTO.getConfernceUnderTakerId() != null) {
			UserInfo confernceUnderTaker = userManagementService
					.findUserInfoByUserInfoId(conferenceStartingDTO
							.getConfernceUnderTakerId());
			conferenceStartingTransientStore
					.setConfernceUnderTaker(confernceUnderTaker);
		}
		if (conferenceStartingDTO.getImportentLevel() != null) {
			DataValue importantLevel = dataDictionaryService
					.findDataValueByDataValueId(conferenceStartingDTO
							.getImportentLevel());
			conferenceStartingTransientStore.setImportantLevel(importantLevel);
		}
		if (conferenceStartingDTO.getNextHandlerId() != null) {
			UserInfo nextApproval = userManagementService
					.findUserInfoByUserInfoId(conferenceStartingDTO
							.getNextHandlerId());
			conferenceStartingTransientStore.setNextApproval(nextApproval);
		}
		if (conferenceStartingDTO.getPlanEndTime() != null) {
			conferenceStartingTransientStore
					.setPlanEndTime(conferenceStartingDTO.getPlanEndTime());
		}

		return conferenceStartingTransientStore;
	}

	private ConferenceStartingDTO makeEntityToDto(
			ConferenceStartingTransientStore conferenceStartingTransientStore)
			throws BusinessException {
		if (conferenceStartingTransientStore == null) {
			return null;
		}
		ConferenceStartingDTO conferenceStartingDTO = new ConferenceStartingDTO();
		conferenceStartingDTO = dozer.map(conferenceStartingTransientStore,
				ConferenceStartingDTO.class);
		if (conferenceStartingTransientStore.getTransientStoreId() != null) {
			conferenceStartingDTO
					.setConferenceStartingDTOId(conferenceStartingTransientStore
							.getTransientStoreId());
		}
		if (conferenceStartingTransientStore.getConferenceCreator() != null) {
			conferenceStartingDTO
					.setConferenceCreatorId(conferenceStartingTransientStore
							.getConferenceCreator().getUserInfoId());
		}
		if (conferenceStartingTransientStore.getConfernceUnderTaker() != null) {
			conferenceStartingDTO
					.setConfernceUnderTakerId(conferenceStartingTransientStore
							.getConfernceUnderTaker().getUserInfoId());
		}
		if (conferenceStartingTransientStore.getImportantLevel() != null) {
			conferenceStartingDTO
					.setImportentLevel(conferenceStartingTransientStore
							.getImportantLevel().getDataValueId());
		}
		if (conferenceStartingTransientStore.getJobId() != null) {
			Job job = taskManagementService
					.findJobDetailByJobId(conferenceStartingTransientStore
							.getJobId());
			String missionCode = job.getMission().getMissionCode();
			conferenceStartingDTO.setMissionCode(missionCode);
		}
		if (conferenceStartingTransientStore.getNextApproval() != null) {
			conferenceStartingDTO
					.setNextHandlerId(conferenceStartingTransientStore
							.getNextApproval().getUserInfoId());
		}
		if (conferenceStartingTransientStore.getPlanEndTime() != null) {
			conferenceStartingDTO
					.setPlanEndTime(conferenceStartingTransientStore
							.getPlanEndTime());
		}

		return conferenceStartingDTO;
	}

	@Override
	public ConferenceStartingDTO findTransientInfoById(Long transientInfoId)
			throws BusinessException {
		Assert.isTrue(transientInfoId!=null,"transientInfoId不能为null");
		ConferenceStartingTransientStore conferenceStartingTransientStore = conferenceTransientStoreService
				.findTransientInfoById(transientInfoId);
		if (conferenceStartingTransientStore == null) {
			return null;
		} else {
			return makeEntityToDto(conferenceStartingTransientStore);
		}
	}

	@Override
	public void deleteTransientInfoById(Long transientInfoId)
			throws BusinessException {
		Assert.isTrue(transientInfoId!=null,"transientInfoId不能为null");
		conferenceTransientStoreService.deleteTransientInfoById(transientInfoId);
	}

}
